const mysql = require('../config/db')
const { DataTypes } = require('sequelize')
require('dotenv').config()

const Post = mysql.define(
  'posts',
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM('draft', 'published', 'archived'),
      defaultValue: 'draft',
    },
    metaTitle: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    metaDescription: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    keywords: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    coverImage: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    excerpt: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    wordCount: {
      type: DataTypes.BIGINT,
      defaultValue: 0,
    },
    allowComment: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
    },
    views: {
      type: DataTypes.BIGINT,
      defaultValue: 0,
    },
    likes: {
      type: DataTypes.BIGINT,
      defaultValue: 0,
    },
    createdAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
    updatedAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
    visibility: {
      type: DataTypes.ENUM('public', 'private', 'password'),
      defaultValue: 'public',
    },
    password: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    categoryId: {
      type: DataTypes.BIGINT,
      allowNull: true,
    },
    userId: {
      type: DataTypes.BIGINT,
      allowNull: false,
    },
    isTop: {
      type: DataTypes.ENUM('N', 'Y'),
      allowNull: false,
      defaultValue: 'N',
    },
  },
  {
    tableName: 'posts',
    timestamps: true,
    paranoid: true,
    deletedAt: process.env.DELETED_AT,
  },
)

module.exports = Post
